<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Marks extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model(array('students_model', 'marks_model', 'user_profiles_model', 'classes_model'));
        $this->load->helper(array('form'));
    }

    function index() {
        if(!in_array($this->tank_auth->get_role(), array('student', 'student_association', 'class_representative')))
            redirect('welcome');

        $user_id = $this->tank_auth->get_user_id();
		if($this->tank_auth->get_role() == 'student')
		{
			$class_id = $this->students_model->get_by_id($user_id)->class_id;
			$marks = $this->marks_model->get_student_marks($user_id,$class_id);

			foreach($marks as $mark)
				$data['marks'][] = $this->_get_mark_data($mark);
		}
		elseif($this->tank_auth->get_role() == 'teacher')
		{
			
		} else redirect('welcome');
		
		$this->ingesup->render('marks/index', $data);
	}

    function admin() {
        if(!in_array($this->tank_auth->get_role(), array('teacher', 'student_association', 'class_representative')))
            redirect('welcome');

        $events = $this->events_model->get_by_user($this->tank_auth->get_user_id());

        foreach($events as $mark)
            $data['events'][$mark->id] = $this->_get_event_data($mark);

        if($this->tank_auth->is_role('teacher')) {
            $classes = $this->classes_model->get_by_teacher($this->tank_auth->get_user_id());

            foreach($classes as $class) {
                $events = $this->events_model->get_by_user_for_class($this->tank_auth->get_user_id(), $class->id);
                $data['classes'][$class->id] = array();

                foreach($events as $mark)
                    $data['classes'][$class->id][$mark->id] = $this->_get_event_data($mark);
            }
        } else {
            $class_id = $this->students_model->get_by_id($this->tank_auth->get_user_id())->class_id;
            $events = $this->events_model->get_by_user_for_class($this->tank_auth->get_user_id(), $class_id);
            $data['classes'][$class_id] = array();

            foreach($events as $mark)
                $data['classes'][$class_id][$mark->id] = $this->_get_event_data($mark);
        }

        $this->ingesup->render('events/admin', $data);
    }

    function view($event_id) {
        $data['mark'] = $this->_get_event_data($this->events_model->get_by_id($event_id));

        if(empty($data['mark']))
            redirect('welcome');

        $this->ingesup->render('events/view', $data);
    }

    function edit($event_id=null) {
        if(isset($event_id))
            $data['mark'] = $this->_get_event_data($this->events_model->get_by_id($event_id));
        else
            $data = '';
        $this->ingesup->render('events/edit', $data);
    }

    function delete($event_id) {
        $this->events_model->delete($event_id);
        $this->admin();
    }

    function link($class_id, $event_id) {
        $this->events_model->link($class_id, $event_id);
        $this->admin();
    }

    function unlink($class_id, $event_id) {
        $this->events_model->unlink($class_id, $event_id);
        $this->admin();
    }

    function show($class_id, $event_id) {
        $this->events_model->show($class_id, $event_id);
        $this->admin();
    }

    function mask($class_id, $event_id) {
        $this->events_model->mask($class_id, $event_id);
        $this->admin();
    }

    function _get_mark_data($mark) {
        $data = array(
            'eval_id' => $mark->eval_id,
            'student' => $this->user_profiles_model->get_user_fullname($mark->student_id),
            'date' => date('d/m/Y', strtotime($mark->date)),
            'heure' => date('H:i:s', strtotime($mark->date)),
            'type' => $mark->type,
          //  'cours' => $mark->title,
            'title' => $mark->titre,
            'student_id' => $mark->student_id,
            'Evaluation' => $mark->titre,
            'note' => $mark->note
        );

        if(isset($mark->last_update)) {
            $data['last_update_date'] = date('d/m/Y', strtotime($mark->last_update));
            $data['last_update_time'] = date('H:i:s', strtotime($mark->last_update));
            $data['updated'] = true;
        } else
            $data['updated'] = false;

        if(isset($mark->published)) {
            $data['publish_date'] = date('d/m/Y', strtotime($mark->published));
            $data['publish_time'] = date('H:i:s', strtotime($mark->published));
            $data['published'] = true;
        } else
            $data['published'] = false;

        return $data;
    }
}